class Solution(object):
    def minSwap(self, nums1, nums2):
        n = len(nums1)
        now0 = 0
        now1 = 1
        for i in range(1, n):
            if nums1[i - 1] <= nums2[i - 1]:
                if nums1[i] <= nums2[i - 1]:
                    next0 = now0
                    next1 = now1 + 1
                else:
                    if nums2[i] <= nums2[i - 1]:
                        next0 = now1
                        next1 = now0 + 1
                    else:
                        next0 = min(now0, now1)
                        next1 = min(now0, now1) + 1
            else:
                if nums1[i] <= nums1[i - 1]:
                    next0 = now1
                    next1 = now0 + 1
                else:
                    if nums2[i] <= nums1[i - 1]:
                        next0 = now0
                        next1 = now1 + 1
                    else:
                        next0 = min(now0, now1)
                        next1 = min(now0, now1) + 1
            now0 = next0
            now1 = next1
        return min(now0, now1)


data = Solution()
nums1 = [0, 7, 8, 10, 10, 11, 12, 13, 19, 18]
nums2 = [4, 4, 5, 7, 11, 14, 15, 16, 17, 20]
print(data.minSwap(nums1, nums2))